#include "fileOperationsHeader.h"
#include "hashTable.h"
char* createDataStructures(int);
char* searchFile(char *name,char *outputFile)
{
	if(mh==NULL)
		return ERR_VFS_SEARCHFILE_03;
	FILE *fpOut=fopen(outputFile,"wb");
	if(fpOut==NULL)
		return ERR_VFS_SEARCHFILE_01;
	int currentFileDescriptors=mh->countOfFileDescriptors;
	int maxFileDescriptors=mh->maxDescriptors;
	int noOfBlocks=maxFileDescriptors/2;
	int hashval = hashfunc(hashtable,name[0]);
	node *bucket=hashtable->table[hashval];
	char *noFileFound;
	if(bucket==NULL)
	{
		noFileFound=(char*)malloc(sizeof(char)*50);
		sprintf(noFileFound,"%s","No files/directories found with the given pattern");
		fwrite(noFileFound,strlen(noFileFound),1,fpOut);
		fwrite("\n",1,1,fpOut);
		//fclose(fpOut);
		//char *ret=createDataStructures(maxFileDescriptors);
		return "SUCCESS";
	}
	int flag=0;
	while(bucket!=NULL)
	{
		if(strncmp(bucket->filename,name,strlen(name))==0)
		{
			char *foundFile=(char*)malloc(sizeof(char)*(strlen(bucket->filename)+strlen(bucket->filePath)+2));
			if(strcmp(bucket->filePath,"/")==0)
				sprintf(foundFile,"%s%s",bucket->filePath,bucket->filename);
			else
				sprintf(foundFile,"%s/%s",bucket->filePath,bucket->filename);
			fwrite(foundFile,strlen(foundFile),1,fpOut);
			fwrite("\n",1,1,fpOut);
			flag=1;
		}
		bucket=bucket->next;
	}
	if(!flag)
	{
		noFileFound=(char*)malloc(sizeof(char)*50);
		sprintf(noFileFound,"%s","No files/directories found with the given pattern");
		fwrite(noFileFound,strlen(noFileFound),1,fpOut);
		fwrite("\n",1,1,fpOut);
		//fclose(fpOut);
		//char *ret=createDataStructures(maxFileDescriptors);
		return "SUCCESS";
	}
	//char *ret=createDataStructures(maxFileDescriptors);
	//fclose(fpOut);
	return "SUCCESS";
}
char* createDataStructures(int maxFileDescriptors)
{
	int bstRet=insertBSTWhileMount(maxFileDescriptors,h->desc);
	if(bstRet!=0)
		return "FAILURE <while creating BST>";
	int nAryRet=createnAryTree(maxFileDescriptors,h->desc);
	if(nAryRet!=0)
		return "FAILURE <while creating nAryTree>";
	//traversenAryTree(nAryRoot);
	int hashTableRet=hashTableMethod(h->desc,maxFileDescriptors);
	if(hashTableRet!=0)
		return "FAILURE <while creating hashTable>";
}
